const categoryService = require("../services/categoryService");
const { successResponse, errorResponse } = require("../utils/response");

// 获取所有分类
exports.getAllCategories = async (ctx) => {
  try {
    const categories = await categoryService.getAllCategories();
    ctx.body = successResponse(categories, "获取所有分类成功");
  } catch (error) {
    ctx.body = errorResponse(error);
  }
};

// 获取单个分类通过id
exports.getCategoryById = async (ctx) => {
  try {
    const category = await categoryService.getCategoryById(ctx.params.id);
    ctx.body = successResponse(category, "获取分类成功");
  } catch (error) {
    ctx.body = errorResponse(error, "获取分类失败");
  }
};

// 创建新的分类
exports.createCategory = async (ctx) => {
  try {
    const category = await categoryService.createCategory(ctx.request.body);
    ctx.body = successResponse(category, "创建分类成功");
  } catch (error) {
    ctx.body = errorResponse(error, "创建分类失败");
  }
};

// 更新分类
exports.updateCategory = async (ctx) => {
  try {
    const category = await categoryService.updateCategory(
      ctx.params.id,
      ctx.request.body,
    );
    ctx.body = successResponse(category, "更新分类成功");
  } catch (error) {
    ctx.body = errorResponse(error, "更新分类失败");
  }
};

// 删除分类
exports.deleteCategory = async (ctx) => {
  try {
    await categoryService.deleteCategory(ctx.params.id);
    ctx.body = successResponse(true, "删除分类成功");
  } catch (error) {
    ctx.body = errorResponse(error, "删除分类失败");
  }
};
